//
//  MusicStyleCell.swift
//  NDAi
//
//  Created by 丸子 on 2024/7/25.
//

import UIKit
import YYText
class MusicStyleCell: UITableViewCell {
    var stlyeList:[MusicStyle] = []
    var chooseDesc:((String) -> Void)?
    var chooseStyle:((String) -> Void)?

    lazy var lineView: UIView = {
        let view = UIView()
        view.backgroundColor = Theme.color.main
        view.allRound(4)
        return view
    }()
    
    lazy var titleLb: UILabel = {
        let label = UILabel()
        label.font = UIFont.pingFangSCRegular(ofSize: 15)
        label.textColor = Theme.color.main
        label.text = "音乐风格"
        return label
    }()
    
    lazy var descView: UIView = {
        let view = UIView()
        view.allRound(8)
        view.backgroundColor = .white
        return view
    }()
    
    lazy var descTextView: YYTextView = {
        let view = YYTextView()
        view.textContainerInset = UIEdgeInsets(top: 8, left: 12, bottom: 10, right: 12);
        view.placeholderText = "请输入音乐风格，如：摇滚"
        view.placeholderFont = UIFont.pingFangSCRegular(ofSize: 14)
        view.placeholderTextColor = Theme.color.text_d3
        view.textColor = Theme.color.text_d1
        view.delegate = self
        view.font = UIFont.pingFangSCRegular(ofSize: 14)
        view.isEditable = false // 禁止用户编辑
        view.isScrollEnabled = true // 允许滚动
        return view
    }()
    
    lazy var buttonBtn: UIButton = {
        let button = UIButton(type: .custom)
        button.imageForNormal = UIImage(named: "icon_music_auto")
        button.titleForNormal = "随机生成风格"
        button.titleFont = UIFont.pingFangSCRegular(ofSize: 14)
        button.titleColorForNormal = Theme.color.text_d1
        return button
    }()
    
    // 使用懒加载创建UICollectionView
    lazy var collectionView: UICollectionView = {
        // 创建一个UICollectionViewFlowLayout实例来设置布局
        let layout = UICollectionViewFlowLayout()
        layout.scrollDirection = .horizontal // 设置为水平滚动
        layout.sectionInset = UIEdgeInsets(top: 0, left: 16, bottom: 0, right: 16)
        layout.minimumInteritemSpacing = 8
        // 创建UICollectionView实例
        let collectionView = UICollectionView(frame: .zero, collectionViewLayout: layout)
        collectionView.backgroundColor = .clear
        // 注册Cell类
        collectionView.register(cellWithClass: TMMusicStyleItem.self)
        // 设置数据源和代理
        collectionView.dataSource = self
        collectionView.delegate = self
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.showsVerticalScrollIndicator = false
        return collectionView
    }()
    
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        backgroundColor = .clear
        selectionStyle = .none
        contentView.addSubviews([lineView, titleLb, descView])
        descView.addSubviews([descTextView, collectionView, buttonBtn])
        
        lineView.snp.makeConstraints { make in
            make.top.equalTo(16)
            make.leading.equalTo(16)
            make.height.equalTo(16)
            make.width.equalTo(6)
        }
        
        titleLb.snp.makeConstraints { make in
            make.centerY.equalTo(lineView)
            make.leading.equalTo(lineView.snp.trailing).offset(8)
        }
        
        descView.snp.makeConstraints { make in
            make.top.equalTo(lineView.snp.bottom).offset(12)
            make.leading.trailing.equalToSuperview().inset(16)
            make.height.equalTo(240)
        }
        
        descTextView.snp.makeConstraints { make in
            make.top.leading.trailing.equalToSuperview()
            make.height.equalTo(130)
        }
        
        collectionView.snp.makeConstraints { make in
            make.top.equalTo(descTextView.snp.bottom).offset(0)
            make.leading.trailing.equalToSuperview()
            make.bottom.equalTo(buttonBtn.snp.top).offset(-6)
        }
        
        buttonBtn.snp.makeConstraints { make in
            make.bottom.trailing.equalToSuperview().inset(12)
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setstylelist(list:[MusicStyle]) {
        self.stlyeList = list
        self.collectionView.reloadData()
    }
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        kHideKeyboard()
    }
}

extension MusicStyleCell: YYTextViewDelegate {
    func textViewDidChange(_ textView: YYTextView) {
        let currentLength = textView.text.count
        if textView == self.descTextView {
            if currentLength > 500 {
                textView.text = String(textView.text.prefix(500))
            }
            self.chooseDesc?(textView.text)
        } else {
            if currentLength > 50 {
                textView.text = String(textView.text.prefix(50))
            }
        }
    }
    func textViewDidEndEditing(_ textView: YYTextView) {
         
    }
}
extension MusicStyleCell: UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return self.stlyeList.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let model = self.stlyeList[indexPath.item]
        let cell = collectionView.dequeueReusableCell(withClass: TMMusicStyleItem.self, for: indexPath)
        cell.titleLb.text = model.title
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let model = self.stlyeList[indexPath.item]
//        if !self.descTextView.text.isEmpty {
//            self.descTextView.text += ",\(titleStr)"
//        }else {
//            self.descTextView.text += "\(titleStr)"
//        }
        self.chooseStyle?(model.title ?? "")
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let model = self.stlyeList[indexPath.item]
        let text = model.title ?? "" // 从数据源获取文本
        let size = (text as NSString).size(withAttributes: [.font: UIFont.pingFangSCMedium(ofSize: 14)])
        let width = size.width + 16 // 为文本添加一些边距
        return CGSize(width: width, height: 30) // 50 是 cell 的固定高度
    }
}

 
class TMMusicStyleItem: UICollectionViewCell {
    
    lazy var titleLb:UILabel = {
        let label = UILabel()
        label.text = "AAA"
        label.textColor = UIColor(hexString: "#7D6EFF")
        label.font = UIFont.pingFangSCMedium(ofSize: 14)
        label.textAlignment = .center
        return label
    }()
     
    override init(frame: CGRect) {
        super.init(frame: frame)
        allRound(15)
        backgroundColor = UIColor(hexString: "#7D6EFF", transparency: 0.1)
        contentView.addSubviews([titleLb])
        titleLb.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
